class Solution {
public:
    void nextPermutation(vector<int>& nums)
    {
        reverse(nums.begin(), nums.end());
        int i = 1;
        while (i < nums.size() && nums[i - 1] <= nums[i])
        {
            ++i;
        }
        //i++;
        if (i < nums.size())
        {
            int j = 0;
            while (j < i && nums[j] <= nums[i])
            {
                ++j;
            }
            swap(nums[i], nums[j]);
        }
        reverse(nums.begin(), nums.begin() + i);
        reverse(nums.begin(), nums.end());

    }
};